<?php
/**
 * 分页导航 通用类 暂时提供了4中通用的分页方法；
 * 类初始化后：$link->show(3);
 * by wangxian
 * 2008.12.22
 * 
 * @param int $cpage //当前页码
 * @param int $pagenum //每页显示显示多少条数据
 * @param int $totaldata //总数据
 * @param string $url //链接地址
 * @param int $opage //每次显示多少页码
 */
class link {
	private $cpage; //当前页码
	private $pagenum; //每页数据数
	private $totaldata; //总数据数
	private $url; //url前一截
	private $opage; //每次显示的页码数
	function __construct($cpage, $pagenum, $totaldata, $url, $opage = 10) {
		$this->cpage = $cpage;
		if ($this->cpage == '' || $this->cpage < 2) {
			$this->cpage = 1;
		}
		$this->totaldata = $totaldata;
		$this->url = $url;
		$this->totalpage = ceil ( $totaldata / $pagenum );
		
		if ($this->totalpage == 0) {
			
			$this->totalpage = 1;
		
		}
		$this->opage = $opage;
		//print_r($_SERVER);
	

	}
	/*
	 * 模式选择
	 */
	function show($style) {
		switch ($style) {
			case 1 : //最简单的上一页 下一些 
				return $this->f1 ();
				break;
			case 2 : //一次翻翻N页的 link
				return $this->f2 ();
				break;
			case 3 : //滑动滚动
				return $this->f3 ();
				break;
			case 4 :
				return $this->f4 ();
				break;
			case 5 :
				return $this->f5 ();
				break;
		}
	}
	/**
	 * url路由函数
	 *
	 * @return string $urlstr
	 * //$link=new link($_GET['cpage'],5,49,array('soft_down','-','.html'));
	 *
	 *	//url 普通模式 
	 *	$link=new link($_GET['cpage'],5,149,array('demo.php','?'));
	 */
	function url($tpage) {
		//$filedir = dirname ( $_SERVER ['SCRIPT_NAME'] );
		if ($this->url [0] && $this->url [1]) {
			if ($this->url [2]) {
				$urlstr = $this->url [0] . $this->url [1] . $tpage . $this->url [2];
			} else {
				$urlstr = $this->url [0] . $this->url [1] . 'page=' . $tpage;
			}
		
		} else {
			echo 'URL ROUTE IS ERROR！～';
		}
		//echo $urlstr;
		return $urlstr;
	}
	function f1() {
		//echo $this->totalpage;
		//最简单的上一页 下一些 
		

		//echo $this->url;
		//echo $this->cpage;
		//echo $this->totalpage;
		$__linkstr = "\n<p class=\"links\">\n"; //begin
		if ($this->totalpage < 2) {
			$nextpage = $this->cpage + 1;
			$__linkstr .= "<span class=\"nolinks\">首页</span>" . " \n";
			$__linkstr .= "<span class=\"nolinks\">上一页</span> \n";
			$__linkstr .= "<span class=\"nolinks\">下一页</span> \n";
			$__linkstr .= "<span class=\"nolinks\">尾页</span> \n";
		} elseif ($this->cpage < 2) {
			//首页
			$nextpage = $this->cpage + 1;
			$__linkstr .= "<span class=\"nolinks\">首页</span>" . " \n";
			$__linkstr .= "<span class=\"nolinks\">上一页</span> \n";
			$__linkstr .= "<a href=\"" . $this->url ( $nextpage ) . "\">下一页</a> \n";
			$__linkstr .= "<a href=\"" . $this->url ( $this->totalpage ) . "\">尾页</a> \n";
		} elseif ($this->cpage >= $this->totalpage) {
			//＝尾页
			$prepage = $this->cpage - 1;
			
			$__linkstr .= "<a href=\"" . $this->url ( 1 ) . "\">首页</a> \n";
			$__linkstr .= "<a href=\"" . $this->url ( $prepage ) . "\">上一页</a> \n";
			$__linkstr .= "\n<span class=\"nolinks\">下一页</span> \n";
			$__linkstr .= "<span class=\"nolinks\">尾页</span>" . " \n";
		} else {
			//正常
			

			$prepage = $this->cpage - 1;
			$nextpage = $this->cpage + 1;
			
			$__linkstr .= "<a href=\"" . $this->url ( 1 ) . "\">首页</a> \n";
			$__linkstr .= "<a href=\"" . $this->url ( $prepage ) . "\">上一页</a> \n";
			$__linkstr .= "\n<a href=\"" . $this->url ( $nextpage ) . "\">下一页</a> \n";
			$__linkstr .= "<a href=\"" . $this->url ( $this->totalpage ) . "\">尾页</a> \n";
		}
		$__linkstr .= "</p>\n"; //end div
		return $__linkstr;
	
	}
	function f2() {
		//一次翻翻N页的 link
		

		$p1 = ceil ( ($this->cpage - $this->opage) / $this->opage );
		
		/**
		 * 计算开始页 结束页
		 */
		$beginpage = $p1 * ($this->opage) + 1;
		$endpage = ($p1 + 1) * ($this->opage);
		if ($endpage > $this->totalpage) { //最后一页 大于总页数
			$endpage = $this->totalpage;
		}
		
		//echo $beginpage.$endpage;;
		//echo $beginpage - $this->opage;
		/**
		 * 前后滚10页
		 */
		$preopage = ($beginpage - $this->opage > 0) ? $beginpage - $this->opage : ''; //上一个N页码
		$nextopage = ($beginpage + $this->opage < $this->totalpage) ? $beginpage + $this->opage : ''; //下一个N页码
		

		$__linkstr = "\n<p class=\"links\">\n"; //begin	
		//分页
		$__linkstr .= "分页:{$this->cpage}/{$this->totalpage} \n";
		//前滚10页码
		if ($preopage) {
			$__linkstr .= "<a href=\"" . $this->url ( $preopage ) . "\"><<前滚{$this->opage}页</a> \n";
		} else {
			$__linkstr .= "<span class=\"nolinks\">前滚{$this->opage}页</span> \n";
		}
		
		//主要的数字分页 页码		
		for($i = $beginpage; $i <= $endpage; $i ++) {
			if ($this->cpage != $i) {
				$__linkstr .= "<a href=\"{$this->url($i)}\">" . $i . "</a> \n";
			} else {
				$__linkstr .= "<span class=\"nolinks\">{$i}</span> \n";
			}
		}
		//后滚10页码
		if ($nextopage) {
			$__linkstr .= "<a href=\"{$this->url($nextopage)}\">后滚{$this->opage}页>></a> \n";
		} else {
			$__linkstr .= "<span class=\"nolinks\">后滚{$this->opage}页</span> \n";
		}
		
		$__linkstr .= "</p>\n"; //end div
		return $__linkstr;
	}
	
	function f3() {
		//滑动滚动
		/**
		 * 计算开始页 结束页
		 */
		if ($this->cpage > ceil ( ($this->opage) / 2 )) {
			$beginpage = $this->cpage - ceil ( ($this->opage) / 2 );
			$endpage = $this->cpage + ceil ( ($this->opage) / 2 ) - 1;
		} else {
			$beginpage = 1;
			$endpage = $this->opage;
		}
		
		//限制末页
		if ($endpage > $this->totalpage) {
			$endpage = $this->totalpage;
		}
		
		$__linkstr = "\n<p class=\"links\">\n"; //begin
		

		//分页
		$__linkstr .= "分页:{$this->cpage}/{$this->totalpage} \n";
		
		//首页
		if ($this->cpage > 1) {
			$__linkstr .= "<a href=\"{$this->url(1)}\">首页</a> \n";
			$__linkstr .= "<a href=\"" . $this->url ( $this->cpage - 1 ) . "\">上一页</a> \n";
		} else {
			$__linkstr .= "<span class=\"nolinks\">首页</span>" . " \n";
			$__linkstr .= "<span class=\"nolinks\">上一页</span>" . " \n";
		}
		
		//main num. link		
		for($i = $beginpage; $i <= $endpage; $i ++) {
			if ($this->cpage != $i) {
				$__linkstr .= "<a href=\"{$this->url($i)}\">" . $i . "</a> \n";
			} else {
				$__linkstr .= "<span class=\"nolinks\">{$i}</span> \n";
			}
		}
		//尾页
		if ($this->cpage == $this->totalpage || $this->totalpage == 0) {
			$__linkstr .= "<span class=\"nolinks\">下一页</span>" . " \n";
			$__linkstr .= "<span class=\"nolinks\">尾页</span>" . " \n";
		} else {
			$__linkstr .= "<a href=\"" . $this->url ( $this->cpage + 1 ) . "\">下一页</a> \n";
			$__linkstr .= "<a href=\"" . $this->url ( $this->totalpage ) . "\">尾页</a> \n";
		}
		$__linkstr .= "</p>\n"; //end div
		return $__linkstr;
	}
	

	function f4() {
		//翻页返回 数组 可以用图片的
		//@return array
		

		//echo $this->url;
		//echo $this->cpage;
		//echo $this->totalpage;
		if ($this->totalpage < 2) {
			//=总页数是1
			$__linkstr [] = array ('', '首页' );
			$__linkstr [] = array ('', '下一页' );
			$__linkstr [] = array ('', '下一页' );
			$__linkstr [] = array ('', '尾页' );
		} elseif ($this->cpage < 2) {
			//=首页
			$__linkstr [] = array ('', '首页' );
			$__linkstr [] = array ('', '下一页' );
			$__linkstr [] = array (($this->cpage + 1), '下一页' );
			$__linkstr [] = array ($this->totalpage, '尾页' );
		
		} elseif ($this->cpage >= $this->totalpage) {
			//＝尾页
			

			$__linkstr [] = array (1, '首页' );
			$__linkstr [] = array (($this->cpage - 1), '上一页' );
			$__linkstr [] = array ('', '下一页' );
			$__linkstr [] = array ('', '尾页' );
		} else {
			//=正常
			

			$__linkstr [] = array (1, '首页' );
			$__linkstr [] = array (($this->cpage - 1), '上一页' );
			$__linkstr [] = array (($this->cpage + 1), '下一页' );
			$__linkstr [] = array (($this->totalpage), '尾页' );
		}
		return $__linkstr;
	
	}
	function f5() {
		//wap分页

		
		$__linkstr = "\n<p class=\"links\">\n"; //begin
		

		//下一页 尾页
		if ($this->cpage == $this->totalpage || $this->totalpage == 0) {
			$__linkstr .= "<span class=\"nolinks\">下一页</span>" . " \n";
		} else {
			$__linkstr .= "<a href=\"" . $this->url ( $this->cpage + 1 ) . "\">下一页</a> \n";
		}
		$__linkstr .=' / ';
		//首页
		if ($this->cpage > 1) {
			$__linkstr .= "<a href=\"" . $this->url ( $this->cpage - 1 ) . "\">上一页</a> \n";
		} else {
			$__linkstr .= "<span class=\"nolinks\">上一页</span>" . " \n";
		}
		
		$__linkstr .='<br />';
		
		//数字分页
		if($this->totalpage<7)
		{
			for($i=1;$i<$this->totalpage+1;$i++)
			{  
				if($this->cpage == $i)
					$__linkstr .=  $i." \n";
				else 
					$__linkstr .= "<a href=\"{$this->url($i)}\">{$i}</a> \n";
			}
		}
		elseif($this->cpage < 4 && $this->totalpage>7)
		{
			for($i=1;$i<5;$i++)
			{  
				if($this->cpage == $i)
					$__linkstr .=  $i." \n";
				else 
					$__linkstr .= "<a href=\"{$this->url($i)}\">{$i}</a> \n";
			}
			$__linkstr .= "..";
			$__linkstr .= "<a href=\"{$this->url($this->totalpage)}\">{$this->totalpage}</a> \n";
		}
		elseif($this->cpage >= 4 && $this->totalpage > $this->cpage + 4)
		{
			$beginpage = $this->cpage - ceil ( ($this->opage) / 2 );
			for($i=$this->cpage - 2;$i<=$this->cpage+1;$i++)
			{  
				if($this->cpage == $i)
					$__linkstr .=  $i." \n";
				else 
					$__linkstr .= "<a href=\"{$this->url($i)}\">{$i}</a> \n";
			}
			$__linkstr .= "..";
			$__linkstr .= "<a href=\"{$this->url($this->totalpage)}\">{$this->totalpage}</a> \n";
		}
		elseif($this->totalpage <= $this->cpage + 4)
		{
			$beginpage = $this->cpage - ceil ( ($this->opage) / 2 );
			for($i=$this->totalpage-7;$i<=$this->totalpage;$i++)
			{  
				if($this->cpage == $i)
					$__linkstr .=  $i." \n";
				else 
					$__linkstr .= "<a href=\"{$this->url($i)}\">{$i}</a> \n";
			}
		}

		$__linkstr.='
			到<input name="goPageNo" format="*N" size="2" value="" maxlength="3" emptyok="true"/>页
			<anchor>
			<go href="'. $this->url('') .'" method="post" sendreferer="true">
				<postfield name="page" value="$goPageNo"/>
			</go>跳转
			</anchor>
		';
		$__linkstr .= "</p>\n"; //end div
		return $__linkstr;
	}
	function __destruct() {
		unset ( $this->cpage );
		unset ( $this->totalpage );
		unset ( $this->totaldata );
		unset ( $this->url );
		unset ( $this->opage );
	}
}
/**
function getQueryString(){
	$_SGETS = explode("/",substr($_SERVER['PATH_INFO'],1));
	$_SLEN = count($_SGETS);
	$_SGET = $_GET;
	for($i=0;$i<$_SLEN;$i+=2){
		if(!empty($_SGETS[$i]) && !empty($_SGETS[$i+1])) $_SGET[$_SGETS[$i]]=$_SGETS[$i+1];
	}
	return $_SGET;
}
 */
/*
////////////////////////////////debug
//new link('当前页数','一页显示的数量','总数据量','URl','每次显示的页数');
//url 关于url array('url地址','url分割方式如：[?-]','.html');
$link=new link($_GET['cpage'],5,49,array($_SERVER[SCRIPT_NAME],'?'),6);
echo <<<at
<style>
*{font-size:12px;color:#333;}
</style>
at;
echo "<br />";
echo '展示样式1 普通上下页 $link->show(1)：'.$link->show(1);

echo "<br />";
echo '展示样式2 批量翻页 $link->show(2)：'.$link->show(2);

echo "<br />";
echo '展示样式3 滑动滚动 $link->show(3)：'.$link->show(3);

echo "<br />";
echo '展示样式5 灵活的 上一页 下一页 print_r($link->show(4))：<br />';
echo '返回的：<pre>';
print_r($link->show(4));
echo '</pre>';

echo '展示样式4 灵活的 上一页 下一页 返回的：<pre>';
print_r($link->show(5));
echo '</pre>';
*/

?>